package com.lwq.controller;

import java.util.Iterator;

import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;

import com.lwq.cocos.CCLayer;
import com.lwq.cocos.CCNode;
import com.lwq.cocos.CCSence;
import com.lwq.cocos.CCSprite;
import com.lwq.cocos.ProjectInfoAdpter;
import com.lwq.model.SuperTableItemModel;
import com.lwq.model.SuperTableModel;
import com.lwq.util.Constance;
import com.lwq.util.ReflectUtil;
import com.lwq.view.CenterEditorView;
import com.lwq.view.CreateMemberDialog;
import com.lwq.view.CreateMemberDialog.GetDataListener;
import com.lwq.view.CreateNodeDialog;
import com.lwq.view.CreateNodeDialog.GetNameListener;
import com.lwq.view.RightEditorView;

public class ProjectController {

	public static void projectAddSceneCommand() {
		CreateNodeDialog dialog = new CreateNodeDialog("New Sence", new GetNameListener() {
			@Override
			public void getDate(String name) {
				// TODO Auto-generated method stub
				newSceneCommand(name);
			}
		});
	}

	public static void projectAddLayoutCommand() {
		CreateNodeDialog dialog = new CreateNodeDialog("New Layout", new GetNameListener() {
			@Override
			public void getDate(String name) {
				// TODO Auto-generated method stub
				newLayoutCommand(name);
			}
		});
	}

	public static void projectAddSpriteCommand() {
		CreateNodeDialog dialog = new CreateNodeDialog("New Sprite", new GetNameListener() {
			@Override
			public void getDate(String name) {
				// TODO Auto-generated method stub
				newSpriteCommand(name);
			}
		});
	}

	public static void projectAddActionCommand() {

	}

	public static void projectRemoveCommand() {
		int option = JOptionPane.showConfirmDialog(null, "Really remove this Component?", "remove",
				JOptionPane.YES_NO_OPTION);
		if (option == 0) {
			ProjectInfoAdpter.getInstance().removeNode();
		}
	}

	public static void newSceneCommand(String name) {
		CCSence sence = new CCSence(name);
		ProjectInfoAdpter.getInstance().addNode(sence);
	}

	public static void newLayoutCommand(String name) {
		CCLayer layout = new CCLayer(name);
		ProjectInfoAdpter.getInstance().addNode(layout);
	}

	public static void newSpriteCommand(String name) {
		CCSprite sprite = new CCSprite(name);
		ProjectInfoAdpter.getInstance().addNode(sprite);
	}

	public static void TreeSelectNodeCommand(DefaultMutableTreeNode selNode) {
		ProjectInfoAdpter.getInstance().setSelectNode(selNode);
		CCNode node = ProjectInfoAdpter.getInstance().getSelectNode();
		if (node == null)
			return;
		SuperTableModel superModel = Constance.getSuperTableModel(node.getType());
		String key = null;
		Object objVal = null;
		String value = null;
		String method = null;
		SuperTableItemModel itemModel = null;
		if (superModel != null) {
			//add exp members
			if(node.getExpMap() != null && node.getExpMap().size() > 0){
				itemModel = new SuperTableItemModel();
				String memName = null;
				Iterator<String> iterator = node.getExpMap().keySet().iterator();
				while(iterator.hasNext()){
					memName = iterator.next();
					itemModel.setKey(memName);
					itemModel.setType(Constance.SUPER_TABLE_EXP_TYPE);
					superModel.addItem(itemModel);
				}
				
			}
			
			for (int i = 0; i < superModel.getItemCount(); i++) {
				itemModel = superModel.getItem(i);
				if(!itemModel.getType().equalsIgnoreCase(Constance.SUPER_TABLE_EXP_TYPE)){
					key = itemModel.getKey();
					method = Constance.GET_STR + key;
					objVal = ReflectUtil.reflect(node, method);
					if (objVal != null) {
						value = objVal.toString();
						itemModel.setResult(value);
					}
				}
			}
			RightEditorView.getInstance().setEditorBoard(superModel);
		}
		CenterEditorView.getInstance().repaint();
		RightEditorView.getInstance().repaint();

	}

	public static void UpdateDataCommand() {
		SuperTableModel model = RightEditorView.getInstance().getEditorResult();
		CCNode node = ProjectInfoAdpter.getInstance().getSelectNode();
		SuperTableItemModel itemModel = null;
		String key = null;
		String value = null;
		String method = null;
		if (node != null && model != null) {
			for (int i = 0; i < model.getItemCount(); i++) {
				itemModel = model.getItem(i);
				key = itemModel.getKey();
				value = itemModel.getResult();
				method = Constance.SET_STR + key;
				ReflectUtil.reflect(node, method, String.class, value);
			}
			node.init();
			CenterEditorView.getInstance().repaint();
		}
	}

	public static void addMemberCommand() {
		new CreateMemberDialog("Create member", new GetDataListener() {

			@Override
			public void getDate(String data) {
				// TODO Auto-generated method stub
				CCNode node = ProjectInfoAdpter.getInstance().getSelectNode();
				if (data == null || node == null)
					return;
				node.expAddMember(data);
				ProjectController.TreeSelectNodeCommand(ProjectInfoAdpter.getInstance().getSelectTreeNode());
			}
		});
	}
	
	public static void editMemberCommand(final String name) {
		new CreateMemberDialog("Edit member", new GetDataListener() {
			@Override
			public void getDate(String data) {
				// TODO Auto-generated method stub
				CCNode node = ProjectInfoAdpter.getInstance().getSelectNode();
				if (data == null || node == null)
					return;
				node.expEditMember(name, data);
				ProjectController.TreeSelectNodeCommand(ProjectInfoAdpter.getInstance().getSelectTreeNode());
			}
		});
	}
}
